<?php

require_once '../../includes/az_mysqli.php';
require_once '../../classes/az_News.php';

run_action();


/*
 *  Run action data;
 */
function run_action() {
    if (isset($_POST['act']) && $_POST['act'] != null) {
        $action = $_POST['act'];
    } else if (isset($_GET['act']) && $_GET['act'] != null) {
        $action = $_GET['act'];
    } else {
        $action = 0;
    }
    $html = null;
    switch ($action) {
        case 1:
            if(add_new()){
               header('Location: index.php');
            }
            break;
        case 2:
            if(update_new()){
                header('Location: index.php');
            }
            break;
        case 3:
            if(delete_new()){
                header('Location: index.php');
            }
          break;
        case 4:
            break;
        case 5:
            break;
        default:
            break;
    }
    /* Send as JSON */
    //header("Content-Type: application/json", true);
    //echo json_encode($html);
    //exit();
}

/*
 * Function to add new post
 * @return true or false
 */

function add_new() {
      if (isset($_POST['title']) && is_string($_POST['title']) && isset($_POST['textnew']) && $_POST['textnew'] != '') {
        $title = trim($_POST['title']);
        $author = trim($_POST['author']);
        $descripton = trim($_POST['newdes']);
        $tags = trim($_POST['newtags']);
        $content = trim($_POST['textnew']);
        
        //Check stt new
        if (isset($_POST['checksttnew']) && is_array($_POST['checksttnew'])) {
            foreach ($_POST['checksttnew'] as $value) {
                if ($value == 'savedraff') {
                    $active = 0;
                }
                if ($value == 'public') {
                    $active = 1;
                }
                if ($value == 'hot') {
                    $hot = 1;
                }
                if ($value == 'allowcom') {
                    $allowcom = 1;
                }
            }
        }else{
            $active = 0;
            $hot = 0;
            $allowcom = 0;
        }

        //Get category
        if (isset($_POST['categories']) && is_array($_POST['categories'])) {
            $category = '';
            foreach ($_POST['categories'] as $value) {
                $category .= $value . ',';
            }
            if ($category != '') {
                $category = substr($category, 0, -1);
            }
        }else{
            $category = '';
        }
        
        //Get survey id
        if (isset($_POST['survey'])) {
            $survey = $_POST['survey'];
        }else{
            $survey = 0;
        }
        //Get links url image 
        if (isset($_POST['new_link_img']) && $_POST['new_link_img'] != '') {
            $image_links = trim($_POST['new_link_img']);
        }else{
            $image_links = '';
        }
        $slug = url_friendly_for_vietnamese($title);

        //Create object new
        $new = new Az_News();
        $new->createNew(null,$title,$author, $descripton, $content, $image_links,
                        $active, $slug,null, null,1, $allowcom, null, $hot,
                        $survey, $category,$tags);
        return $new->addNews();
    }
}

/*
 * Url friendly
 */

function url_friendly_for_vietnamese($title) {
    $url_pattern = array('`&(amp;|#)?[a-z0-9]+;`i', '`[^a-z0-9]`i');
    $title = htmlentities($title, ENT_COMPAT, 'utf-8');
    $title = preg_replace('`&([a-z]+)(acute|uml|circ|grave|ring|cedil|slash|tilde|caron|lig);`i', "\\1", $title);
    $title = preg_replace('`\[.*\]`U', '', $title);
    $title = strtolower(trim($title, '-'));

    $title = preg_replace("/(à|á|ạ|ả|ã|â|ầ|ấ|ậ|ẩ|ẫ|ă|ằ|ắ|ặ|ẳ|ẵ)/", 'a', $title);
    $title = preg_replace("/(è|é|ẹ|ẻ|ẽ|ê|ề|ế|ệ|ể|ễ)/", 'e', $title);
    $title = preg_replace("/(ì|í|ị|ỉ|ĩ)/", 'i', $title);
    $title = preg_replace("/(ò|ó|ọ|ỏ|õ|ô|ồ|ố|ộ|ổ|ỗ|ơ|ờ|ớ|ợ|ở|ỡ)/", 'o', $title);
    $title = preg_replace("/(ù|ú|ụ|ủ|ũ|ư|ừ|ứ|ự|ử|ữ)/", 'u', $title);
    $title = preg_replace("/(ỳ|ý|ỵ|ỷ|ỹ)/", 'y', $title);
    $title = preg_replace("/(đ)/", 'd', $title);
    $title = preg_replace($url_pattern, '-', $title);
    return $title;
}

/*
 * Function to update news
 */
function update_new(){
    if($_POST['newId']!='' && $_POST['title'] !='' && $_POST['textnew'] !=''){
        $newId = $_POST['newId'];
        $title = trim($_POST['title']);
        $content = trim($_POST['textnew']);
        $tags = trim($_POST['newtags']);
        $descr = trim($_POST['newdes']);
        $author = trim($_POST['author']);
         //Check stt new
        if (isset($_POST['checksttnew']) && is_array($_POST['checksttnew'])) {
            foreach ($_POST['checksttnew'] as $value) {
                if ($value == 'savedraff') {
                    $active = 0;
                }
                if ($value == 'public') {
                    $active = 1;
                }
                if ($value == 'hot') {
                    $hot = 1;
                }
                if ($value == 'allowcom') {
                    $allowcom = 1;
                }
            }
        }else{
            $active = 0;
            $hot = 0;
            $allowcom = 0;
        }
        //Get category
        if (isset($_POST['categories']) && is_array($_POST['categories'])) {
            $category = '';
            foreach ($_POST['categories'] as $value) {
                $category .= $value . ',';
            }
            if ($category != '') {
                $category = substr($category, 0, -1);
            }
        }else{
            $category = '';
        }
        
        //Get survey id
        if (isset($_POST['survey'])) {
            $survey = $_POST['survey'];
        }else{
            $survey = 0;
        }
        //Get links url image 
        if (isset($_POST['new_link_img']) && $_POST['new_link_img'] != '') {
            $image_links = trim($_POST['new_link_img']);
        }else{
            $image_links = '';
        }
        $slug = url_friendly_for_vietnamese($title);

        //Create object new
        $new = new Az_News();
        $new->createNew($newId,$title,$author, $descripton, $content, $image_links,
                        $active, $slug,date('d/m/Y'), date('d/m/Y'),1, $allowcom, null, $hot,
                        $survey, $category,$tags);
        return $new->updateNews();
    }
}


/*
 * Function active new
 */
function active_new(){
    
}


/*
 * Function to delete new
 */
function delete_new(){
    if(isset ($_GET['newId']) && is_numeric($_GET['newId'])){
        $new = new Az_News();
        return $new->deleteNew($_GET['newId']);
    }
}

?>
